/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xj.marqueeview;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorGroup;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Component;

/**
 * 动画工具类
 *
 * @author:Hexy
 * @since 2021-03-24
 */
public final class AnimationUtils {
    /**
     * 进入时间
     */
    public static final int ENTRY_TIME = 300;

    /**
     * 停留保持时间
     */
    public static final int MAINTAIN_TIME = 3000;

    /**
     * 持续时间
     */
    public static final int DURATION_TIME = 200;

    /**
     * 起止位置1
     */
    public static final int START_STOP_POSITION1 = 50;

    /**
     * 起止位置2
     */
    public static final int START_STOP_POSITION2 = 100;

    private AnimationUtils() {
    }

    /**
     * 从上进入
     *
     * @param component
     * @return 进入动画
     */
    public static AnimatorProperty getUpInAnimator(Component component) {
        AnimatorProperty upInAnimator = component.createAnimatorProperty();
        return upInAnimator.moveFromY(-START_STOP_POSITION1).moveToY(0).alphaFrom(0).alpha(1)
                .setDelay(ENTRY_TIME).setDuration(DURATION_TIME);
    }

    /**
     * 从下退出
     *
     * @param component
     * @return 退出动画
     */
    public static AnimatorProperty getDownOutAnimator(Component component) {
        AnimatorProperty downOutAnimator = component.createAnimatorProperty();
        return downOutAnimator.moveFromY(0).moveToY(START_STOP_POSITION1).alphaFrom(1).alpha(0)
                .setDelay(MAINTAIN_TIME).setDuration(DURATION_TIME);
    }

    /**
     * 从下进入
     *
     * @param component
     * @return 返回从下进入的动画
     */
    public static AnimatorProperty getDownInAnimator(Component component) {
        AnimatorProperty downInAnimator = component.createAnimatorProperty();
        return downInAnimator.moveFromY(START_STOP_POSITION1).moveToY(0).alphaFrom(0).alpha(1)
                .setDelay(ENTRY_TIME).setDuration(DURATION_TIME);
    }

    /**
     * 从上退出
     *
     * @param component
     * @return 返回从上退出的动画
     */
    public static AnimatorProperty getUpOutAnimator(Component component) {
        AnimatorProperty upOutAnimator = component.createAnimatorProperty();
        return upOutAnimator.moveFromY(0).moveToY(-START_STOP_POSITION1).alphaFrom(1).alpha(0)
                .setDelay(MAINTAIN_TIME).setDuration(DURATION_TIME);
    }

    /**
     * 从左进入
     *
     * @param component
     * @return 返回从左进入的动画
     */
    public static AnimatorProperty getLeftInAnimator(Component component) {
        AnimatorProperty leftInAnimator = component.createAnimatorProperty();
        return leftInAnimator.moveFromX(-START_STOP_POSITION2).moveToX(0).alphaFrom(0).alpha(1)
                .setDelay(ENTRY_TIME).setDuration(DURATION_TIME);
    }

    /**
     * 从右退出
     *
     * @param component
     * @return 返回从右退出的动画
     */
    public static AnimatorProperty getRightOutAnimator(Component component) {
        AnimatorProperty rightOutAnimator = component.createAnimatorProperty();
        return rightOutAnimator.moveFromX(0).moveToX(START_STOP_POSITION2).alphaFrom(1).alpha(0)
                .setDelay(MAINTAIN_TIME).setDuration(DURATION_TIME);
    }

    /**
     * 从右进入
     *
     * @param component
     * @return 返回从右进入的动画
     */
    public static AnimatorProperty getRightInAnimator(Component component) {
        AnimatorProperty rightInAnimator = component.createAnimatorProperty();
        return rightInAnimator.moveFromX(START_STOP_POSITION2).moveToX(0).alphaFrom(0).alpha(1)
                .setDelay(ENTRY_TIME).setDuration(DURATION_TIME);
    }

    /**
     * 从左退出
     *
     * @param component
     * @return 返回从左退出的动画
     */
    public static AnimatorProperty getLeftOutAnimator(Component component) {
        AnimatorProperty leftOutAnimator = component.createAnimatorProperty();
        return leftOutAnimator.moveFromX(0).moveToX(-START_STOP_POSITION2).alphaFrom(1).alpha(0)
                .setDelay(MAINTAIN_TIME).setDuration(DURATION_TIME);
    }

    /**
     * 设置动画组（进入动画，退出动画）
     *
     * @param animators
     * @return 动画组
     */
    public static AnimatorGroup setAnimator(Animator... animators) {
        AnimatorGroup set = new AnimatorGroup();
        set.runSerially(animators);
        set.setLoopedCount(AnimatorGroup.INFINITE);
        return set;
    }
}
